package handle

import (
	"github.com/gin-gonic/gin"
	"new/model"
	"strconv"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	context := c.PostForm("context")
	if title == "" || context == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.Article{
		//Model:   gorm.Model{},
		Title:   title,
		Content: context,
	}
	model.DB.Debug().Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "发表成功",
	})
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	var article []model.Article
	offset := (page - 1) * size
	model.DB.Offset(offset).Limit(size).Find(&article)
	var total int64
	model.DB.Model(&model.Article{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "获取成功",
		"data":  article,
		"total": total,
	})
}
